<?php

	class source
	{

		var $fw		= null;
		var $sec	= null;

		function source(&$framework, &$security)
		{

			$this->fw	= &$framework;
			$this->sec	= &$security;

			$this->fw->pc_item('My Collection', 'Items in your collection', 'index.php?what=mymovies');
			$this->fw->pc_item('Borrow', 'Borrow Management', 'index.php?what=borrow');

			switch($this->fw->do)
			{

				case 'brs': // borrow request status - updates the status' of the selected borrow requests
					$this->brs();
					break;

				case 'request': // someone has requested to borrow a particular movie/s
					$this->borrow_request_form();
					break;

				case 'process_request':
					$this->process_borrow_request();
					break;

				default:
					// this cant be called!
					header('location: ');
					exit;

			}

		}

		// process the own_id's the user wants to borrow
		function process_borrow_request()
		{

			$own_id	= isset($_REQUEST['own_id']) ? $_REQUEST['own_id'] : null;

			$own_id	= !is_array($own_id) ? array($own_id) : $own_id;

			// lets loop through each selected movie, and do some checks
			$member_id	= $this->sec->dtls['id'];

			$movies		= $this->fw->get_member_movies($member_id);
			// array of own_id's of the current member
			$movies_own	= $this->fw->_convert_array($movies, null, 'own_id');
			// array of movie_id's that the current member has in their collection
			$movies_id	= $this->fw->_convert_array($movies, null, 'id');

			foreach($own_id as $own)
			{

				// lets make sure the id isn't our own!
				if(!in_array($own, $movies_own))
				{

					// lets get the movie_id of this movie, and see its already in our collection (pointly borrowing a movie we already have)
					$details = $this->fw->get_movie_own_details($own);

					if(!in_array($details['movie_id'], $movies_id))
					{

						if(!$this->fw->is_borrowing($own, $member_id))
						{

							$sql	= $this->fw->_pq("INSERT INTO #__borrow (movie_id, own_id, owner_id, borrower_id, borrow_request_date) VALUES (%s, %s, %s, %s, UNIX_TIMESTAMP());", $details['movie_id'], $own, $details['member_id'], $member_id);
							$result	= mysql_query($sql) or die(mysql_error());

						}
						else
						{

							// the selected owner copy has previously been requested, or this member already is currently borrowing the selected title

						}
					}
					else
					{

						//the selected movie is already in the member's own collection!
						echo 'the movie you have selected already exists in your collection';

					}

				}
				else
				{

					// the selected movie is the members own movie!
					echo 'the selected movie is your own movie!';

				}

			}

		}

		// displays a form that allows users to request to borrow selected movies
		function borrow_request_form()
		{

			// movies selected, but no particular owner selected!
			$movies_id	= isset($_REQUEST['movie_id']) ? $_REQUEST['movie_id'] : null;

			if(!empty($movies_id))
			{

				$own_id	= isset($_REQUEST['own_id']) ? $_REQUEST['own_id'] : null;

				if(!empty($own_id)) $own_id = !is_array($own_id) ? array($own_id) : $own_id;

				// we have selected movies, but no specific copy of each movie has been selected at this point.
				$movies_id	= !is_array($movies_id) ? array($movies_id) : $movies_id;

				// loop through movies and get their owners
				foreach($movies_id as $v)
				{

					$temp = $this->fw->get_movie_owned_by($v);

					$movies[$v] = count($temp) > 0 ? $temp : false;

				}

				// display a form to handle the selection of the specific movie
				$this->fw->template = 'borrow.request.tpl';

				$this->fw->content['movies'] = $movies;

				if($own_id !== null) $this->fw->content['own_id'] = $own_id;

			}
			else
			{

				// the movies we want to borrow have specificlly been selected, we don't need to choose any owners
				$this->process_borrow_request();

			}

		}

		// update the status of the selected borrow requests
		function brs()
		{

			$status		= $_REQUEST['status'];
			$borrow_id	= $_REQUEST['id'];

		}

	}

?>